package ups.glre.news.dao.impl;


import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.Query;

import ups.glre.news.dao.UserDAO;
import ups.glre.news.model.User;

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class UserDAOImpl extends GenericDAOImpl<User,Integer> implements UserDAO{

	@Override
	public User findUserByLoginAndPassword(String login, String password){
		
		Query q = entityManager.createQuery("SELECT e FROM "
				+ entityClass.getName() + " e WHERE login = :login " +
						" and password = :password ");
		
		q.setParameter("login", login);
		q.setParameter("password", password);
		
		return (User) q.getSingleResult();
	}

	@Override
	public User findUserByLogin(String login) {
		Query q = entityManager.createQuery("SELECT e FROM "
				+ entityClass.getName() + " e WHERE login = :login ");
		
		q.setParameter("login", login);
		
		return (User) q.getSingleResult();
	}
}
